//
//  Common.m
//  CoolTennisBall
//
//  Created by Coollang on 16/7/19.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "Common.h"
#import <pthread.h>


extern inline float OSVersion()
{
    return [[[UIDevice currentDevice] systemVersion] floatValue];
}

extern inline CGRect ApplicationBounds()
{
    return [UIScreen mainScreen].bounds;
}

extern inline CGFloat StatusBarHeight()
{
    CGSize statusSize = UIApplication.sharedApplication.statusBarFrame.size;
    return MIN(statusSize.height, statusSize.width);
}

#pragma mark - Disk Path

extern inline NSString *DocumentPath()
{
    return NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
}

extern inline NSString *CachePath()
{
    return NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
}

extern inline NSString *LibraryPath()
{
    return NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0];
}

extern inline NSString *TempPath()
{
    return NSTemporaryDirectory();
}

#pragma mark - Map String

extern inline NSString *MapString(id obj)
{
    if (obj == nil || obj == [NSNull null] ||
        ([obj isKindOfClass:[NSString class]] && [obj length] == 0))
    {
        return @"";
    }
    else if ([obj isKindOfClass:[NSNumber class]])
    {
        return MapString([obj stringValue]);
    }
    return [obj description];
}

#pragma mark - GCD

extern inline dispatch_time_t yf_dispatch_time_delay(NSTimeInterval second)
{
    return dispatch_time(DISPATCH_TIME_NOW, (int64_t)(second * NSEC_PER_SEC));
}

extern inline BOOL is_main_queue()
{
    return pthread_main_np() != 0;
}

extern inline void dispatch_async_main_queue(void (^block)(void))
{
    if (pthread_main_np()) {
        block();
    } else {
        dispatch_async(dispatch_get_main_queue(), block);
    }
}

extern inline void dispatch_sync_main_queue(void (^block)(void))
{
    if (pthread_main_np()) {
        block();
    } else {
        dispatch_sync(dispatch_get_main_queue(), block);
    }
}

extern inline void dispatch_async_glogle_queue(void (^block)(void))
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block);
}

extern inline void dispatch_sync_glogle_queue(void (^block)(void))
{
    dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block);
}

@implementation Common

// inch 转 cm
+ (CGFloat)inchToCm:(CGFloat)inch {
    return inch * 2.54;
}
+ (CGFloat)cmToInch:(CGFloat)cm {
    return cm / 2.54;
}

// km/h 转mph
+ (CGFloat)kmhToMph:(CGFloat)kmh {
    return  kmh * 0.6214;
}
+ (CGFloat)mphToKmh:(CGFloat)mph {
    return mph / 0.6214;
}

+ (CGFloat)kgToLbs:(CGFloat)kg {
    return kg * 2.2046;
}

+ (CGFloat)lbsTokg:(CGFloat)lbs {
    return lbs / 2.2046;
}

@end

